package com.example.salesservice.web;

import com.example.salesservice.entity.Customer;
import com.example.salesservice.service.CustomerService;
import com.example.salesservice.utils.DataGridView;
import com.example.salesservice.utils.LayuiJson;
import com.example.salesservice.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    LayuiJson layuiJson;

    /**
     * 查询所有客户信息（或根据条件查询）
     * @return 返回JSON语句，数据封装于data中
     */
    @RequestMapping("/loadAllCustomer")
    public LayuiJson<Customer> loadAllCustomer(String customername,String connectionpersion,
                    String phone,Integer page,Integer limit){
        //设置默认值
        page = page == null ? 1 : page;
        limit = limit == null ? 5 : limit;

        int index=(page-1)*limit;
        List<Customer> data = customerService.select(customername, connectionpersion, phone, index, limit);
        //返回JSON
        layuiJson.setCode(0);
        layuiJson.setCount(1000);
        layuiJson.setData(data);
        return layuiJson;
    }

    /**
     * 查询遍历所有客户信息。作为下拉信息
     * @return
     */
    @RequestMapping("loadAllCustomerForSelect")
    public DataGridView loadAllCustomerForSelect(){

        List<Customer> list = customerService.selectName();
        return new DataGridView(list);
    }

    /**
     * 进行一个用户信息的更新
     * @return
     */
    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(Integer id, String customername, String zip, String address, String telephone,
                                    String connectionpersion, String phone, String bank, String account, String email, String fax,
                                    Integer available){
        try {
            //进行一个数据的更新
            customerService.update(id, customername, zip, address, telephone,
                    connectionpersion, phone, bank, account, email, fax, available);

            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @RequestMapping("addCustomer")
    public ResultObj addCustomer(String customername, String zip, String address, String telephone, String connectionpersion,
                                 String phone, String bank, String account, String email, String fax, Integer available){
        try {
            customerService.insert(customername, zip, address, telephone, connectionpersion,
                    phone, bank, account, email, fax, available);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomer(Integer id){
        try {
            customerService.delete(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
